﻿--[[	$Id: caelManager.lua 2623 2012-09-25 19:48:03Z sdkyron@gmail.com $	]]

local _, caelManager = ...

caelManager.eventFrame = CreateFrame("Frame", nil, UIParent)

local playerClass = caelLib.playerClass

local raidBM = "DBM" -- Raid Boss Encounters mod to auto enable

local addons = {
	--[[
	---------------------------------------------------------------------------
	-=( Base Addons )=-
	---------------------------------------------------------------------------
	These addons will load for every user, always.
	--]]

    ["base"] = {
		["!recBug"]					= true,
		["BadBoy"]					= true,
		["caelBags"]				= true,
		["caelBars"]				= true,
		["caelBuffet"]				= true,
		["caelBuffs"]				= true,
		["caelCCBreak"]				= true,
		["caelChat"]				= true,
		["caelCombatLog"]			= true,
		["caelCooldowns"]			= true,
		["caelCore"]				= true,
		["caelDataFeeds"]			= true,
		["caelEmote"]				= true,
		["caelFactions"]			= true,
		["caelInterrupt"]			= true,
		["caelLib"]					= true,
		["caelLoot"]				= true,
		["caelManager"]				= true,
		["caelMap"]					= true,
		["caelMedia"]				= true,
		["caelMinimap"]				= true,
		["caelNameplates"]			= true,
		["caelPanels"]				= true,
		["caelQuests"]				= true,
		["caelThreat"]				= true,
		["caelTimers"]				= true,
		["caelTooltips"]			= true,
		["gotChat"]					= true,
		["gotMacros"]				= true,
		["oUF"]						= true,
		["oUF_Caellian"]			= true,
		["recScrollAreas"]			= true,
		["Snoopy"]					= true,
		["TomTom"] = true,
		["WoWPro"] = true,
		["WoWPro_Leveling"] = true,
		["onyxfox_guildxp"] = true,
    },

	--[[
	---------------------------------------------------------------------------
	-=( Class-Specific Addons )=-
	---------------------------------------------------------------------------
	These addons will load for the specified class, always.
	--]]

	["HUNTER"]  = {
		["caelMisdirection"]		= true,
		["caelPetCare"]				= true,
	},
	["ROGUE"]   = {
		["caelTrick"]				= true,
	},

	--[[
	---------------------------------------------------------------------------
	-=( Task-Specific Addons )=-
	---------------------------------------------------------------------------
	These addons will load if you use their set name after the slash command.
	"/addonset raid" to load raid addons, for example.
	--]]

	["raid"] = {
		["caelBossWhisperer"]		= true,
		["alDamageMeter"]			= true,
		["caelGroupCD"]				= true,
		["recThreatMeter"]			= true,
	},

	["BigWigs-core"] = {
		["BigWigs"]					= true,
		["BigWigs_Core"]			= true,
		["BigWigs_Options"]			= true,
		["BigWigs_Plugins"]			= true,
		["BigWigs_Foreign"]			= true,
	},

	["BigWigs-zone"] = {
		["Baradin Hold"]				= "BigWigs_Baradin",
		["Bastian of Twilight"]			= "BigWigs_Bastion",
		["Blackwing Descent"]			= "BigWigs_Blackwing",
		["Dragon Soul"]					= "BigWigs_DragonSoul",
		["Firelands"]					= "BigWigs_Firelands",
		["Throne of the Four Winds"]	= "BigWigs_Throne",
	},

	["DBM-core"] = {
		["DBM-Core"]				= true,
		["DBM-GUI"]					= true,
		["DBM-WorldEvents"]			= true,
	},

	["DBM-zone"] = {
		["Baradin Hold"]				= "DBM-BaradinHold",
		["Bastian of Twilight"]			= "DBM-BastionTwilight",
		["Blackwing Descent"]			= "DBM-BlackwingDescent",
		["Dragon Soul"]					= "DBM-DragonSoul",
		["Firelands"]					= "DBM-Firelands",
		["Throne of the Four Winds"]	= "DBM-ThroneFourWinds",
	},

	["party"] = {
		["alDamageMeter"]			= true,
		["caelGroupCD"]				= true,
		["DBM-Party-BC"]			= true,
		["DBM-Party-Cataclysm"]		= true,
		["DBM-Party-WotLK"]			= true,
		["recThreatMeter"]			= true,
	},

	["pvp"] = {
		["caelGroupCD"]				= true,
		["DBM-PvP"]					= true,
	},

	["dev"] = {

	},
}

local caelUIAddons = {}
for setName, set in pairs(addons) do
	for addon, v in pairs(set) do
		if (type(v) == "string") then
			addon = v
		end
		caelUIAddons[addon] = true
	end
end

StaticPopupDialogs["RELOAD_UI"] = {
	text			= "Reload the UI to update |cffD7BEA5cael|rManager",
	button1			= ACCEPT,
	button2			= CANCEL,
	OnAccept		= ReloadUI,
	timeout			= 0,
	hideOnEscape	= false
}

local enableSet = function(set)

	if not addons[set] then return end

	for k, v in pairs(addons[set]) do
		if v == true then
			EnableAddOn(k)
		elseif v == false then
			DisableAddOn(k)
		end
	end
end

local changeSet = function(set, zone)

	for i = 1, GetNumAddOns() do
		if caelUIAddons[GetAddOnInfo(i)] then
			DisableAddOn(i)
		end
	end

	enableSet("base")

	if set then

		if zone and zone == "" then
			zone = GetRealZoneText()
		end

		if set == "arena" then set = "pvp" end

		if set == "raid" then
			enableSet(set)

			if addons[raidBM .. "-zone"][zone] and addons[raidBM .. "-core"] then
				enableSet(raidBM .. "-core")

				for _, mod in pairs(addons[raidBM .. "-zone"]) do
					EnableAddOn(mod)
				end
			end
		elseif set == "party" or set == "pvp" then
			enableSet(set)

			if addons[raidBM .. "-core"] then
				enableSet(raidBM .. "-core")
			end
		else
			enableSet(set)
		end
	end

	enableSet(playerClass)

	StaticPopup_Show("RELOAD_UI")
end

local autoSwitch = function(zone)
	if UnitIsDeadOrGhost("player") then return end

	local _, instanceType = IsInInstance()

	if instanceType ~= "none" then
		for k, v in pairs(addons[instanceType]) do
			local enabled = select(4, GetAddOnInfo(k))
			if (v == true and not enabled) or (v == false and enabled) then
				changeSet(instanceType, zone)
				break
			end
		end
	else
		for i = 1, GetNumAddOns() do
			local name, _, _, enabled = GetAddOnInfo(i)

			if caelUIAddons[name] then
                enabled = not not enabled
                local shouldBeEnabled = not not (addons.base[name] or (addons[playerClass] and addons[playerClass][name]))

                if shouldBeEnabled ~= enabled then
                    changeSet()
                end
			end
		end
	end
end

caelManager.eventFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
caelManager.eventFrame:SetScript("OnEvent", function(self)
	autoSwitch(GetRealZoneText())
end)

local handleSlash = function(set)
	changeSet(set, GetRealZoneText())
end

SLASH_CAELADDONSMANAGER1 = "/addonset"
SlashCmdList.CAELADDONSMANAGER = handleSlash